package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.common.util.ExceptionUtil;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.search.client.SearchFeignClient;

import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {
    private final SearchFeignClient searchFeignClient;
    private final XxlJobLogMapper xxlJobLogMapper;
    private final UserInfoFeignClient userInfoFeignClient;

    public DispatchJobHandler(SearchFeignClient searchFeignClient,
                              XxlJobLogMapper xxlJobLogMapper,
                              UserInfoFeignClient userInfoFeignClient) {
        this.searchFeignClient = searchFeignClient;
        this.xxlJobLogMapper = xxlJobLogMapper;
        this.userInfoFeignClient = userInfoFeignClient;
    }
    /**
     * @description: xxl_jog 定时任务 更新排行榜
     * @author Petrichor
     * @date 2024/1/12 19:00
    **/
    @XxlJob(value = "updateLatelyAlbumRanking")
    public void task01() {
        XxlJobLog xxlJobLog = new XxlJobLog();
        log.error("xxl_jog 定时任务 更新排行榜===============================================");
        // 1>任务配置id
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        // 定时任务 执行开始
        long startTime = System.currentTimeMillis();
        try {
            // 更新排行榜
            searchFeignClient.updateLatelyAlbumRanking();
            // 2>任务状态   成功
            xxlJobLog.setStatus(1);

        } catch (Exception e) {
            // 2>任务状态   失败
            xxlJobLog.setStatus(0);
            // 3>失败信息
            xxlJobLog.setError(ExceptionUtil.getErrorMessage(e));
            log.error("定时任务执行失败，任务id为：{}", XxlJobHelper.getJobId());
            e.printStackTrace();
        } finally {
            // 4>耗时(单位：毫秒)
            int times = (int) (System.currentTimeMillis() - startTime);
            xxlJobLog.setTimes(times);
            xxlJobLogMapper.insert(xxlJobLog);
        }

    }
    /**
     * @description: xxl_jog 更新Vip到期失效状态
     * @author Petrichor
     * @date 2024/1/12 19:00
     **/
    @XxlJob("updateVipExpireStatusJob")
    public void updateVipExpireStatusJob() {

        log.info("更新Vip到期失效状态：{}", XxlJobHelper.getJobId());

        //记录定时任务相关的日志信息
        XxlJobLog xxlJobLog = new XxlJobLog();
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        long startTime = System.currentTimeMillis();

        try {

            // 远程调用service-user微服务接口更新vip失效状态
            userInfoFeignClient.updateVipExpireStatus();
            xxlJobLog.setStatus(1);     //成功

        } catch (Exception e) {
            xxlJobLog.setStatus(0);     //失败
            xxlJobLog.setError(ExceptionUtil.getErrorMessage(e));
            log.error("定时任务执行失败，任务id为：{}", XxlJobHelper.getJobId());
            e.printStackTrace();
        } finally {
            int times = (int) (System.currentTimeMillis() - startTime);
            xxlJobLog.setTimes(times);
            xxlJobLogMapper.insert(xxlJobLog);
        }

    }
}